<?php
/**
 * Created by PhpStorm.
 * User: Marmot
 * Date: 2018/5/10
 * Time: 10:51
 */
namespace app\admin\controller;

use app\admin\model\Auth;
use app\admin\model\Common;

class AuthFun extends Base{
    //功能权限列表
    public function lst(){
        $auth = new Auth();
        $res = $auth->get_auth_role();
//        dump($res);
        $this->assign('auth_fun',$res);
        return $this->fetch();
    }
    //添加角色
    public function add(){
        if ($this->request->isPost()){
            $data = $this->request->param();
            $validate_error = $this->validate(
                $data,
                ['role_name'=> 'require'],
                ['role_name.require' => '角色名称必须']
            );
            if (true !== $validate_error){
                $this->error($validate_error);
            }else{
                $auth = new Auth();
                $r = $auth->add_auth_role($data);
            }
            if ($r){
                $this->success('添加成功','auth_fun/lst');
            }else{
                $this->error('添加失败');
            }
        }else{
            $nav = new Common();
            $nav_arr = $nav->get_nav();
            $this->assign('nav_arr',$nav_arr);
            return $this->fetch();
        }
    }
    //修改角色
    public function edit(){
        $auth = new Auth();
        $id = $this->request->param('id');
        if ($this->request->isPost()){
            $data['role_name'] = $this->request->param('role_name');
            $data['role_res'] = $this->request->param('role_res/a');
            $data['role_res'] = json_encode($data['role_res']);
            $r = $auth->updata_auth_role($data,'id='.$id);
            if ($r){
                $this->success('修改成功','authFun/lst');
            }else {
                $this->error('修改失败');
            }
        }else{
            $nav = new Common();
            $nav_arr = $nav->get_nav();
            $this->assign('nav_arr',$nav_arr);

            $role_arr = $auth->get_auth_role('id='.$id,false);
            $this->assign('role_arr',$role_arr[0]);
        }
        return $this->fetch();
    }

    public function delete(){
        $id = $this->request->param('id');
        $auth = new Auth();
        $r = $auth->table('d_auth_fun_role')->where('id='.$id)->delete();
        if ($r){
            $this->success('删除成功','authFun/lst');
        }else{
            $this->error('删除失败');
        }
        return $this->fetch();
    }

}